#include <iostream>

using namespace std;

const int N = 1e6 + 1;
int li[N];

int main()
{
	
	int n, h;
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> li[i];
	cin >> h;
	for(int i = h; i; i = li[i])
		cout << i << " ";
	return 0;
 } 
